package com.cl.base.auth.handler;

import com.cl.base.auth.service.JwtTokenService;
import com.cl.base.util.JsonUtil;
import com.cl.base.util.ResultVo;
import com.cl.base.util.ServletUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义退出处理类 返回成功
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class ClLogoutSuccessHandler implements LogoutSuccessHandler {
    private final JwtTokenService jwtTokenService;
    private final JsonUtil jsonUtil;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        String token = jwtTokenService.getToken(request);
//        PayloadDto payloadDto = jwtTokenService.getPayloadDtoByHMAC(token);
//        LoginUser loginUser = tokenService.getLoginUser(request);
//        if (StringUtils.isNotNull(loginUser))
//        {
//            String userName = loginUser.getUsername();
//             删除用户缓存记录
//            tokenService.delLoginUser(loginUser.getToken());
//             记录用户退出日志
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "退出成功"));
//        }
        ServletUtils.renderString(response, jsonUtil.toString(ResultVo.ok("退出成功")));
    }
}
